package com.haogu.service.impl;

import com.haogu.entity.Announcement;
import com.haogu.entity.Department;
import com.haogu.entity.VAnnouncement;
import com.haogu.mapper.AnnouncementMapper;
import com.haogu.service.Announcements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnnouncementsImpl implements Announcements {
    @Resource
    private AnnouncementMapper announcementMapper;

    /**
     * 显示所有公告
     *
     * @return
     */
    @Override
    public Map selectAnnouncement(Integer page, Integer rows, String title, String departmentid) {
        //根据标题、部门进行模糊查询
        if (title != null && "".equals(title) || departmentid != null && "".equals(departmentid)) {
            List<VAnnouncement> list =
                    announcementMapper.selectLike((page - 1) * rows, rows, title, departmentid);
            //设置当前状态及登录界面状态
            for (VAnnouncement vas : list) {
                if (vas.getStatus().equals("1")) {
                    vas.setStatus("发布中!");
                } else {
                    vas.setStatus("未发布!");
                }
                if (vas.getIsshow().equals("1")) {
                    vas.setIsshow("显示!");
                } else {
                    vas.setIsshow("不显示!");
                }
            }
            Map map1 = new HashMap();
            Integer rows1 = announcementMapper.selectCount(null);

            map1.put("totalRows", rows1);
            map1.put("result", list);
            Map map2 = new HashMap();
            map2.put("data", map1);
            return map2;
        }

        //查询所有公告
        Map map1 = new HashMap();
        //查询公告总数
        Integer rows1 = announcementMapper.selectCount(null);
        //查询所有公告
        List<VAnnouncement> vAnnouncements =
                announcementMapper.selectAnnouncement((page - 1) * rows, rows);

        //设置当前状态及登录界面状态
        for (VAnnouncement va : vAnnouncements) {
            if (va.getStatus().equals("1")) {
                va.setStatus("发布中");
            } else {
                va.setStatus("未发布");
            }
            if (va.getIsshow().equals("1")) {
                va.setIsshow("显示");
            } else {
                va.setIsshow("不显示");
            }
        }

        map1.put("totalRows", rows1);
        map1.put("result", vAnnouncements);
        Map map2 = new HashMap();

        map2.put("data", map1);
        return map2;
    }

    /**
     * 查询某条公告
     * @param id
     * @return
     */
    @Override
    public Announcement selectOneAnnous(Integer id) {
        return announcementMapper.selectOneAnnous(id);
    }

    /**
     * 部门查询
     * @return
     */
    @Override
    public List<Department> selectDepartment() {
        return announcementMapper.selectDepartment();
    }

    /**
     * 添加公告
     * @param announcement
     * @return
     */
    @Override
    public Map insertAnnous(Announcement announcement) {
        int insert = announcementMapper.insert(announcement);
        Map map = new HashMap();

        if (insert == 1) {
            map.put("success", true);
            map.put("message", "添加成功！");
        } else {
            map.put("success", false);
            map.put("message", "添加失败！");
        }
        return map;
    }

    /**
     * 修改
     * @param announcement
     * @return
     */
    @Override
    public int changes1(Announcement announcement) {
        return announcementMapper.updateById(announcement);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int del(Integer id) {
        return announcementMapper.deleteById(id);
    }

    /**
     * 查询公告总数
     * @return
     */
    @Override
    public int selectCounts() {
        return announcementMapper.selectCounts();
    }
}
